﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data;

namespace SSISForSAPComponents
{
    public class Util
    {
        /// <summary>
        /// 转换SAPTable到DataTable结构
        /// </summary>
        /// <param name="sapTable"></param>
        /// <returns></returns>
        public static System.Data.DataTable ConventSapTableToDataTable(com.sap.mw.jco.JCO.Table sapTable,string tableName)
        {
            DataTable table = new DataTable(tableName);
            //建立table结构
            for (int j = 0; j < sapTable.getFieldCount(); j++)
            {
                DataType comDataType = Util.ConventSapTypeToNetType(sapTable.getType(j));
                table.Columns.Add(sapTable.getName(j), (comDataType == DataType.DT_DECIMAL ? typeof(Decimal) : typeof(String)));  
            }
            return table;
        }
        /// <summary>
        /// 将DataTable转入SAPTable
        /// </summary>
        /// <param name="sapTable"></param>
        /// <param name="table"></param>
        public static void ConventDataTableToSapTable(ref com.sap.mw.jco.JCO.Table sapTable, DataTable table)
        {
            foreach (DataRow dr in table.Rows)
            {
                sapTable.appendRow(); 
                for (int j = 0; j < sapTable.getFieldCount(); j++)
                {
                    //com.sap.mw.jco.JCO.Structure 
                    if(dr[sapTable.getName(j)]!=null ) 
                    {
                        sapTable.setValue(dr[sapTable.getName(j)], sapTable.getName(j));
                    }
                }
            }
        }

        /// <summary>
        /// 转换SAP类型到.NET类型
        /// </summary>
        public  static  DataType ConventSapTypeToNetType(int sapType)
        {

            Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
            switch (sapType)
            {

                case 0: // 'C' 类型 
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 1: //'D'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_DATE ;
                    break;
                case 0x62: //'E'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 7://'F'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;

                case 8: //'I'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 6://'N' num数字类型
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 2: //BDC类型 数字
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_DECIMAL;
                    break;
                case 3: //'T'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR ;
                    break;
                case 4://'X'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 10: // 'b'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 0x1d://'g'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 0x63: //'h'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 9://'s'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 0x11://'u' 'v'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                case 30://'y'
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
                default:
                    netType = Microsoft.SqlServer.Dts.Runtime.Wrapper.DataType.DT_WSTR;
                    break;
            }
            return netType;
        }
    }
}
